package com.train.daily.mapper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.train.common.model.page.PageSorted;
import com.train.common.resp.PageResp;
import com.train.daily.dto.train.DailyTrainCodeDTO;
import com.train.daily.dto.train.DailyTrainPageDTO;
import com.train.daily.po.DailyTrainPO;
import com.train.daily.resp.train.DailyTrainPageResp;
import com.train.daily.resp.train.DailyTrainQueryResp;
import com.train.mybatis.base.MyBaseMapper;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

/**
 * @author baitiaojun
 * @description 针对表【dailyTrain(每日车次)】的数据库操作Mapper
 * @createDate 2025-02-28 17:55:24
 * @Entity generator.domain.DailyTrain
 */
public interface DailyTrainMapper extends MyBaseMapper<DailyTrainPO> {

    default PageResp<DailyTrainPageResp> pageList(DailyTrainPageDTO dailyTrainPageDTO) {
        LambdaQueryWrapper<DailyTrainPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(dailyTrainPageDTO.getCode()), DailyTrainPO::getCode, dailyTrainPageDTO.getCode())
                .eq(ObjectUtil.isNotNull(dailyTrainPageDTO.getDate()), DailyTrainPO::getDate, dailyTrainPageDTO.getDate());
        PageResp<DailyTrainPO> dailyTrainPOPageResp = this.selectPage(
                dailyTrainPageDTO,
                queryWrapper,
                new PageSorted(DailyTrainPO::getUpdateTime),
                new PageSorted(DailyTrainPO::getCreateTime));
        List<DailyTrainPageResp> dailyTrainPageRespList = BeanUtil.copyToList(dailyTrainPOPageResp.getData(), DailyTrainPageResp.class);
        return new PageResp<>(dailyTrainPOPageResp.getTotal(), dailyTrainPageRespList);
    }

    default boolean existsId(Long id) {
        LambdaQueryWrapper<DailyTrainPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainPO::getId, id);
        return this.exists(queryWrapper);
    }

    default boolean existsDailyTrain(String code, LocalDate date) {
        LambdaQueryWrapper<DailyTrainPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainPO::getCode, code).eq(DailyTrainPO::getDate, date);
        return this.exists(queryWrapper);
    }

    default List<DailyTrainCodeDTO> getDailyTrainCode(LocalDate date) {
        LambdaQueryWrapper<DailyTrainPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(DailyTrainPO::getCode, DailyTrainPO::getId)
                .eq(DailyTrainPO::getDate, date).groupBy(DailyTrainPO::getCode);
        List<DailyTrainPO> dailyTrainPOS = this.selectList(queryWrapper);
        return BeanUtil.copyToList(dailyTrainPOS, DailyTrainCodeDTO.class);
    }

    default boolean deleteTrains(LocalDate date, List<String> trainCodes) {
        LambdaQueryWrapper<DailyTrainPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainPO::getDate, date).in(DailyTrainPO::getCode, trainCodes);
        return this.delete(queryWrapper) > 0;
    }

    default boolean existsDailyTrain(LocalDate date, List<String> trainCodes) {
        LambdaQueryWrapper<DailyTrainPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainPO::getDate, date).in(DailyTrainPO::getCode, trainCodes).last("limit 1");
        return this.exists(queryWrapper);
    }

    default LocalTime getDailyTrain(LocalDate date, String trainCode) {
        LambdaQueryWrapper<DailyTrainPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainPO::getCode, trainCode).eq(DailyTrainPO::getDate, date);
        DailyTrainPO dailyTrainPO = this.selectOne(queryWrapper);
        return dailyTrainPO.getStartTime();
    }

    default DailyTrainQueryResp queryTrain(String trainCode, LocalDate date) {
        LambdaQueryWrapper<DailyTrainPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .select(DailyTrainPO::getStart, DailyTrainPO::getStartTime, DailyTrainPO::getEndTime, DailyTrainPO::getEnd)
                .eq(StrUtil.isNotEmpty(trainCode), DailyTrainPO::getCode, trainCode)
                .eq(ObjectUtil.isNotNull(date), DailyTrainPO::getDate, date);
        DailyTrainPO dailyTrainPO = this.selectOne(queryWrapper);
        return BeanUtil.copyProperties(dailyTrainPO, DailyTrainQueryResp.class);
    }
}